import 'dart:math';

import 'package:flutter/material.dart';
import 'package:tencent_cloud_chat_sdk/models/v2_tim_message.dart';
import 'package:video_ducafecat_flutter_v3/common/index.dart';

class SoundElemWidget extends StatelessWidget {
  const SoundElemWidget({
    super.key,
    required this.message,
    required this.isSelf,
    required this.isPlaying,
    required this.onPlaySound,
  });

  final V2TimMessage message;
  final bool isSelf;
  final bool isPlaying;
  final Function(V2TimMessage message) onPlaySound;

  // 获取语音框显示长度
  double _getSoundLen(int? duration) {
    int charLen = 8;
    double soundLen = 32;
    if (duration != null) {
      final realSoundLen = duration;
      int sdLen = 32;
      if (realSoundLen > 10) {
        sdLen = 12 * charLen + ((realSoundLen - 10) * charLen / 0.5).floor();
      } else if (realSoundLen > 2) {
        sdLen = 2 * charLen + realSoundLen * charLen;
      }
      sdLen = min(sdLen, 20 * charLen);
      soundLen = sdLen.toDouble();
    }

    return soundLen;
  }

  // 主视图
  Widget _mainView() {
    final soundElem = message.soundElem;
    final double soundLen = _getSoundLen(soundElem?.duration);

    // final backgroundColor =
    //     isSelf ? AppColors.secondaryContainer : AppColors.tertiary;
    // final fontStyle = TextStyle(
    //     fontSize: 14,
    //     color: isSelf ? AppColors.onSecondaryContainer : AppColors.onTertiary);

    final backgroundColor = AppColors.primary;
    final fontStyle = TextStyle(fontSize: 14, color: AppColors.onPrimary);

    final borderRadius = isSelf
        ? const BorderRadius.only(
            topLeft: Radius.circular(10),
            topRight: Radius.circular(2),
            bottomLeft: Radius.circular(10),
            bottomRight: Radius.circular(10))
        : const BorderRadius.only(
            topLeft: Radius.circular(2),
            topRight: Radius.circular(10),
            bottomLeft: Radius.circular(10),
            bottomRight: Radius.circular(10));

    return InkWell(
      onTap: () => onPlaySound(message),
      // overlayColor: MaterialStateProperty.all(Colors.blue),
      // splashColor: Colors.blue,
      // highlightColor: Colors.blue,
      child: Ink(
        decoration: BoxDecoration(
          color: backgroundColor,
          borderRadius: borderRadius,
        ),
        padding: const EdgeInsets.all(10),
        child: Container(
          // padding: const EdgeInsets.all(10),
          // decoration: BoxDecoration(
          //   color: backgroundColor,
          //   borderRadius: borderRadius,
          // ),
          constraints: const BoxConstraints(maxWidth: 240),
          child: Column(
            children: [
              // Text(message.soundElem?.UUID ?? '-'),
              Row(
                mainAxisSize: MainAxisSize.min,
                children: isSelf
                    ? [
                        Container(width: soundLen),
                        Text(
                          "''${soundElem?.duration} ",
                          style: fontStyle,
                        ),
                        isPlaying
                            ? Image.asset(
                                'assets/chat/images/play_voice_send.gif',
                                width: 16,
                                height: 16,
                              )
                            : Image.asset(
                                'assets/chat/images/voice_send.png',
                                width: 16,
                                height: 16,
                              ),
                      ]
                    : [
                        isPlaying
                            ? Image.asset(
                                'assets/chat/images/play_voice_receive.gif',
                                width: 16,
                                height: 16,
                              )
                            : Image.asset(
                                'assets/chat/images/voice_receive.png',
                                width: 16,
                                height: 16,
                              ),
                        Text(
                          " ${soundElem?.duration}''",
                          style: fontStyle,
                        ),
                        Container(width: soundLen),
                      ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return _mainView();
  }
}
